import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by ScorpionOrange on 2016/10/15.
 */
public class BuggyButtonTest {
    public static void main(String[] args){
        EventQueue.invokeLater(() -> {
            JFrame frame = new BuggyButtonFrame();
            frame.setTitle("BuggyButtonTest");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class BuggyButtonFrame extends JFrame{
    private static final int DEFAULT_WIDTH = 800;
    private static final int DEFAULT_HEIGHT = 600;

    public BuggyButtonFrame(){
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        // add panel to frame
        BuggyButtonPanel panel = new BuggyButtonPanel();
        add(panel);
    }
}

class BuggyButtonPanel extends JPanel{
    public BuggyButtonPanel(){
        ActionListener listener = new ButtonListener();

        JButton yellowButton = new JButton("Yellow");
        add(yellowButton);
        yellowButton.addActionListener(listener);

        JButton blueButton = new JButton("Blue");
        add(blueButton);
        blueButton.addActionListener(listener);

        JButton redButton = new JButton("Red");
        add(redButton);
        redButton.addActionListener(listener);
    }

    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            String arg = event.getActionCommand();
            if(arg.equalsIgnoreCase("yellow")){
                setBackground(Color.yellow);
            }
            else if(arg.equalsIgnoreCase("blue")){
                setBackground(Color.blue);
            }
            else if(arg.equalsIgnoreCase("red")){
                setBackground(Color.red);
            }
        }
    }
}
